package com.meiqiongproperty.yiguanjia.activity;

import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.FragmentActivity;
import android.view.Window;
import android.view.WindowManager;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.commom.YApplication;
import com.readystatesoftware.systembartint.SystemBarTintManager;

/**
 * @author Chen
 * @version V1.0
 * @package com.sameway.cnotes.activity
 * @date 17/1/13
 */
public class BaseFragmentActivity extends FragmentActivity {
    public static BaseFragmentActivity AppInstance;

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        YApplication.getInstance().setCurrentActivity(this);
        super.onCreate(savedInstanceState, persistentState);
        ChangeStatusBar();
    }

    public static BaseFragmentActivity getInstance() {
        return AppInstance;
    }

    /**
     * 结束当前Activity
     */
    public void finishCurrentActivity() {
        finish();
    }

    /**
     * 获取sharedPreferences中的字符串
     *
     * @param spName    要操作的shareprefrences文件名
     * @param parameter 要操作的sharedPreferences Key
     * @return
     */
    protected String getStringFromSP(String spName, String parameter) {
        return getSharePre(spName).getString(parameter, "");
    }

    protected Long getLongFromSP(String spName, String parameter) {
        return getSharePre(spName).getLong(parameter, 0);
    }

    protected int getIntFromSP(String spName, String parameter) {
        return getSharePre(spName).getInt(parameter, 0);
    }

    protected boolean getBooleanFromSP(String spName, String parameter) {
        return getSharePre(spName).getBoolean(parameter, false);
    }

    /**
     * 将字符串值保存到sharedPreferences
     *
     * @param spName 要操作的shareprefrences文件名
     * @param key    要操作的sharedPreferences Key
     * @param value  要操作的sharedPreferences value
     * @return
     */
    protected boolean saveStringToSp(String spName, String key, String value) {
        return getSharePre(spName).edit().putString(key, value).commit();
    }

    /**
     * 将int值保存到sharedPreferences *
     *
     * @param spName 要操作的shareprefrences文件名
     * @param key    要操作的sharedPreferences Key
     * @param value  要操作的sharedPreferences value
     * @return
     */
    protected boolean saveIntToSp(String spName, String key, int value) {
        return getSharePre(spName).edit().putInt(key, value).commit();
    }

    /**
     * 将boolean值保存到sharedPreferences
     *
     * @param spName 要操作的shareprefrences文件名
     * @param key    要操作的sharedPreferences Key
     * @param value  要操作的sharedPreferences value
     * @return
     */
    protected boolean saveBooleanToSp(String spName, String key, Boolean value) {
        return getSharePre(spName).edit().putBoolean(key, value).commit();
    }

    protected boolean saveLongToSp(String spName, String key, long value) {
        return getSharePre(spName).edit().putLong(key, value).commit();
    }

    private SharedPreferences getSharePre(String spName) {
        return getSharedPreferences(spName, 0);
    }

    public void ChangeStatusBar() {
        SystemBarTintManager tintManager;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.transparent);//通知栏所需颜色
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    }
}
